package com.topisv.tms.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.CustomerAreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.repository.AreaGroupDao;
import com.topisv.tms.repository.CustomerAreaGroupDao;
import com.topisv.tms.repository.CustomersDao;
import com.topisv.tms.rest.vo.AreaGroupProVo;
import com.topisv.tms.rest.vo.CustomerAreaGroupVo;
import com.topisv.tms.utils.http.HttpPost;
import com.topisv.tms.utils.vo.RestAreaGroupVo;
import com.topisv.tms.utils.vo.RestCustomersAreagroupVo;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class CustomerAreaGroupService {

	@Autowired
	private CustomerAreaGroupDao customerAreaGroupDao;
	@Autowired
	private CustomersDao customersDao;
	@Autowired
	private AreaGroupDao areaGroupDao;
	
	@Transactional(readOnly = false)
	public CustomerAreaGroup save(CustomerAreaGroup arg0){
		return customerAreaGroupDao.save(arg0);
	}
	
	
	public CustomerAreaGroup findOne(Long id){
		return customerAreaGroupDao.findOne(id);
	}
	
	public EdiOrder setAreaGroupCode(EdiOrder ediOrder){
		//设置网点
		List<CustomerAreaGroup> lists=this.customerAreaGroupDao.findByCustomerCode(ediOrder.getCustomerCode());
		if(ediOrder.getProvco()!=null||!"".equals(ediOrder.getPostco().trim())){
			for(CustomerAreaGroup list:lists){
				String areaLists=list.getAreaLists();
				if(areaLists!=null){
					if(areaLists.indexOf(ediOrder.getProvco())!=-1){
						ediOrder.setAreaGroupCode(list.getAreaGroupCode());
						break;
					}
				}
			}
		}else{
			ediOrder.setGymemo("0");
		}
		return ediOrder;
	}
	
	/**
	 * 同步网点数据
	 * @param customerCode
	 * @throws Exception
	 */
	public void setCustomerAreaGroup(String customerCode)throws Exception{
		try {
			Customers customers=customersDao.findByCustomerCode(customerCode);
			RestCustomersAreagroupVo restCustomersAreagroupVo=HttpPost.getRestCustomersAreagroupVo(customers);
			Date nowDate=new Date();
			if(restCustomersAreagroupVo!=null){
				this.customerAreaGroupDao.deleteCustomerCode(customerCode);
				List<RestAreaGroupVo> restAreaGroupVos=restCustomersAreagroupVo.getRestAreaGroupDtos();
				for(RestAreaGroupVo a:restAreaGroupVos){
					if(null==a.getAreaCodes()||"".equals(a.getAreaCodes().trim())){
						continue;
					}
					CustomerAreaGroup ga=new CustomerAreaGroup();
					ga.setCustomerCode(customerCode);
					ga.setAreaGroupCode(a.getAreagroupCode());
					ga.setUpdateDate(nowDate);
					ga.setAreaLists(a.getAreaCodes());
					ga.setGroupCodes(a.getGroupCodes());
					ga.setLevel(a.getLevel());
					this.customerAreaGroupDao.save(ga);
				}
			}
		} catch (Exception e) {
			throw e;
		}
	}
	
	public RestCustomersAreagroupVo getRestCustomersAreagroupVo(String customerCode){
		Customers customer=customersDao.findByCustomerCode(customerCode);
		List<CustomerAreaGroup> customerAreaGroups=customerAreaGroupDao.findByCustomerCodeOrderByLevelAsc(customerCode);
		RestCustomersAreagroupVo rv=new RestCustomersAreagroupVo();
		List<RestAreaGroupVo> restAreaGroupDtos=new ArrayList<RestAreaGroupVo>();
		for(CustomerAreaGroup customerAreaGroup:customerAreaGroups){
			AreaGroup areaGroup=areaGroupDao.findByGroupCode(customerAreaGroup.getAreaGroupCode());
			RestAreaGroupVo restAreaGroupDto=new RestAreaGroupVo();
			restAreaGroupDto.setAreagroupName(areaGroup.getGroupName());
			restAreaGroupDto.setAreagroupCode(areaGroup.getGroupCode());
			restAreaGroupDto.setAreaCodes(customerAreaGroup.getAreaLists());
			restAreaGroupDto.setLevel(customerAreaGroup.getLevel());
			restAreaGroupDto.setGroupCodes(customerAreaGroup.getGroupCodes());
			restAreaGroupDtos.add(restAreaGroupDto);
		}
		
		rv.setCustomerCode(customerCode);
		rv.setCustomerIntro(customer.getCustomerIntro());
		rv.setCustomerName(customer.getCustomerName());
		rv.setRestAreaGroupDtos(restAreaGroupDtos);
		return rv;
	}
	
	/**
	 * 保存网点数据
	 * @param customerAreaGroupVo
	 */
	@Transactional(readOnly = false)
	public void save(CustomerAreaGroupVo customerAreaGroupVo){
		String customerCode=customerAreaGroupVo.getCustomerCode();
		List<CustomerAreaGroup> cags=new ArrayList<CustomerAreaGroup>();
		this.customerAreaGroupDao.deleteCustomerCode(customerCode);
		List<AreaGroupProVo> areaGroupProVos=customerAreaGroupVo.getAreaGroupProVos();
		Date nowDate=new Date();
		for(AreaGroupProVo a:areaGroupProVos){
			CustomerAreaGroup ga=new CustomerAreaGroup();
			ga.setCustomerCode(customerCode);
			ga.setAreaGroupCode(a.getAreaGroupCode());
			ga.setUpdateDate(nowDate);
			ga.setAreaLists(a.getProLists());
			ga.setLevel(a.getLevel());
			ga.setGroupCodes(a.getNextAreaGrouop());
			cags.add(ga);
		}
		
		this.customerAreaGroupDao.save(cags);
	}
	
	public List<CustomerAreaGroup> findByCustomerCode(String customerCode){
		return customerAreaGroupDao.findByCustomerCode(customerCode);
	}
	
	public CustomerAreaGroup findByCustomerCodeAndAreaGroupCode(String customerCode,String areaGroupCode){
		return customerAreaGroupDao.findByCustomerCodeAndAreaGroupCode(customerCode, areaGroupCode);
	}
}
